
####################################################
####### R code for bank crisis onset prediction ####
####################################################

library(MCMCpack)
library(BMS)
library(dplyr)

######
## BMA MODEL
######

BMA.dat <- read.csv("....bankcrisis_bma_data.csv")
colnames(BMA.dat)

BMA.subdat <- subset(BMA.dat, select = c( "begBC2",  "growth", "logcpi_o", "default", "soe_cred", "civwar", 
                                             "popdensity", "openness", "loggdppc", "gini_mkt_m", 
                                             "logtotresgdp", "log_imfgdp2", "cbi", "logcb_assets",  
                                             "xconst", "leftgov", "rightgov", "leftprop10",
                                             "rightprop10", "logdomcredit", "portgdp" , "logm2",
                                             "log_debtliabgdp", "logcred2dep", 
                                             "logdebtgdp", "KOFFiGIdj",  "KOFFiGIdj_dlag5sum", "KOFEcGIdj_dlag5sum", "KOFEcGIdf_dlag5sum", 
                                             "CAdef", "EquityBankRatio", "FD", "evol_l5m", "peg", "lognomxr", "D_logcpi_o", "D_growth", "D_openness", 
                                             "D_logtotresgdp",   "D_logdomcredit", "D_log_debtliabgdp", 
                                             "D_logcred2dep", "D_logdebtgdp", "D_portgdp", "D_westRate", "D_logcb_assets", 
                                             "D_logm2", "D_KOFFiGIdj", "D_CAdef", "D_EquityBankRatio", "D_FD", "D_logevol", 
                                             "D_lognomxr"))
BMA.main <- na.omit(BMA.subdat)

BMA.main <- rename(BMA.main, Growth  = growth, 
                  log.CPI = logcpi_o, 
                  Default = default, 
                  SOE.cred = soe_cred, 
                  CivWar = civwar, 
                  PopDensity = popdensity, 
                  TrOpenness = openness, 
                  log.GDPPC = loggdppc, 
                  Inequality = gini_mkt_m, 
                  Reserves = logtotresgdp, 
                  IMF.Cred = log_imfgdp2, 
                  CBI = cbi, 
                  CB.assets = logcb_assets,  
                  ExConstraints = xconst, 
                  LeftGov = leftgov, 
                  RightGov = rightgov, 
                  LeftProp = leftprop10,
                  RightProp = rightprop10, 
                  DomCredit = logdomcredit, 
                  PortfolioFlows = portgdp, 
                  log.M2 = logm2,
                  DebtLiabilities = log_debtliabgdp, 
                  BankLeverage = logcred2dep, 
                  DebtGDP = logdebtgdp, 
                  FinanceLiberalized = KOFFiGIdj,  
                  FinancialReform5yr = KOFFiGIdj_dlag5sum, 
                  EconReform5yr = KOFEcGIdj_dlag5sum, 
                  EconFlows5yr = KOFEcGIdf_dlag5sum, 
                  CAdeficit = CAdef, 
                  FinanceDevpmt = FD, 
                  XRvolatile = evol_l5m, 
                  PEG = peg, 
                  XRvalue = lognomxr, 
                  D_CPI = D_logcpi_o, 
                  D_Growth = D_growth, 
                  D_TrOpenness = D_openness, 
                  D_Reserves = D_logtotresgdp,   
                  D_Credit = D_logdomcredit, 
                  D_DebtLiabilites = D_log_debtliabgdp, 
                  D_BankLeverage = D_logcred2dep, 
                  D_DebtGDP = D_logdebtgdp, 
                  D_PortfolioFlows = D_portgdp, 
                  D_WestRate = D_westRate, 
                  D_CBassets = D_logcb_assets, 
                  D_M2 = D_logm2, 
                  D_FinanceLiberal = D_KOFFiGIdj, 
                  D_CAdeficit = D_CAdef, 
                  D_FinanceDevlpmt = D_FD, 
                  D_XRvolatile = D_logevol, 
                  D_XRvalue = D_lognomxr)

set.seed(15973)
bcrisBMA <- bms(BMA.main, burn = 50000, iter = 100000, mcmc = "bd")

### Appendix Figure A1 ###
image(bcrisBMA)

density(bcrisBMA, "CBI")

################
## End Script ##
################


